package com.bingbing.back.myUtils;

import com.bingbing.back.dto.SchoolDist;
import com.bingbing.back.dto.SchoolInfoDto;
import com.bingbing.back.dto.SchoolMajorScoreDto;
import com.bingbing.back.mapper.CityLocMapper;
import com.bingbing.back.po.CityLoc;
import com.bingbing.back.po.SchoolInfo;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class MySort {

    @Resource
    private MyCalcDist myCalcDist;

    @Resource
    private CityLocMapper cityLocMapper;

    // 将学校按照距离最近优先排序
    public List<SchoolInfoDto> sortByDistance(double lon, double la, List<SchoolInfoDto> list){
        List<SchoolInfoDto> result = new ArrayList<>();
        List<SchoolDist> listDist = new ArrayList<>();
        for (SchoolInfo s : list) {
            CityLoc cityLoc = null;
            cityLoc = cityLocMapper.getPos(s.getSchoolProvince(), s.getSchoolCity());
            if(cityLoc == null) continue;

            double dist = myCalcDist.distance(lon, la, cityLoc.getLongitude(), cityLoc.getLatitude());
            SchoolDist schoolDist = new SchoolDist(s);
            schoolDist.setDist(dist);
            listDist.add(schoolDist);
        }
        Comparator comparator = new MyCompare();
        Collections.sort(listDist, comparator);

        listDist.forEach(s->{
            SchoolInfoDto schoolInfoDto = new SchoolInfoDto(s);
            result.add(schoolInfoDto);
        });
        return result;
    }


    public List<SchoolInfoDto> sortBySchoolProbability(List<SchoolInfoDto> list){
        Comparator comparator = new MyCompare1();
        Collections.sort(list, comparator);
        return list;
    }

    public List<SchoolMajorScoreDto> sortByMajorProbability(List<SchoolMajorScoreDto> list){
        Comparator comparator = new MyCompare2();
        Collections.sort(list, comparator);
        return list;
    }
}
